<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>按钮管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20" class="el-row-table">
        <el-col :span="12"
          ><div class="grid-content bg-purple">
            <el-input placeholder="请输入内容" clearable>
              <el-button slot="append" icon="el-icon-search"></el-button>
            </el-input></div
        ></el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <el-button type="primary" @click="addButtonDialogVisible = true"
              >新增按钮</el-button
            >
          </div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <el-button type="success" @click="getButtonList">刷新</el-button>
          </div>
        </el-col>
      </el-row>

      <!-- 按钮列表区域 -->
      <el-table border stripe width="100%" :data="buttonList">
        <el-table-column fixed label="序号" prop="id"> </el-table-column>
        <el-table-column fixed label="按钮名" prop="note"> </el-table-column>
        <el-table-column fixed label="权限标识" prop="name"> </el-table-column>

        <el-table-column fixed label="操作">
          <template slot-scope="scope">
            <!-- 第一个是查看详情按钮 -->
            <!-- <el-tooltip
              effect="dark"
              content="查看"
              placement="top"
              :enterable="false"
            >
              <el-button type="success" icon="el-icon-check" circle></el-button>
            </el-tooltip> -->

            <!-- 第二个是删除按钮 -->
            <el-tooltip
              effect="dark"
              content="删除按钮"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                circle
                @click="removeButton(scope.row.id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.current"
        :page-sizes="[5, 10]"
        :page-size="queryInfo.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 删除按钮对话框 -->
    <el-dialog
      title="删除按钮"
      :visible.sync="removeButtonDialogVisible"
      width="30%"
    >
      <span>确定删除掉按钮：{{ buttonForm.note }} 吗</span>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="removeButtonDialogVisible = false">关 闭</el-button>
        <el-button type="primary" @click="onRemoveButton">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加按钮对话框 -->
    <el-dialog
      title="添加按钮"
      :visible.sync="addButtonDialogVisible"
      width="50%"
      @close="addButtonDialogClosed"
      center
    >
      <!-- 内容主体区 -->
      <el-form
        :model="addButtonForm"
        :rules="addButtonFormRules"
        ref="addButtonRef"
        label-width="100px"
      >
        <el-form-item label="按钮名" prop="note">
          <el-input v-model="addButtonForm.note"></el-input>
        </el-form-item>
        <el-form-item label="按钮标识" prop="name">
          <el-input v-model="addButtonForm.name"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addButtonDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSavaButton">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  pageButton,
  saveButton,
  getButtonById,
  removeButtonById,
} from "@/api/auth.js";
export default {
  data() {
    return {
      //获取按钮列表的参数对象
      queryInfo: {
        current: 1,
        size: 5,
        name: "",
      },
      total: 0,
      buttonList: [],
      //添加按钮窗口显示与隐藏
      addButtonDialogVisible: false,
      addButtonForm: {
        note: "", //按钮注释
        name: "", //按钮名称
      },
      addButtonFormRules: {
        name: [
          {
            required: true,
            message: "请输入按钮名称",
            trigger: "blur",
          },
        ],
        note: [
          {
            required: true,
            message: "请输入按钮备注",
            trigger: "blur",
          },
        ],
      },
      buttonForm: {},
      //删除按钮窗口显示与隐藏
      removeButtonDialogVisible: false,
    };
  },
  created() {
    this.getButtonList();
  },
  methods: {
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.size = newSize;
      this.getButtonList();
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.current = newPage;
      this.getButtonList();
    },
    //获得按钮列表
    async getButtonList() {
      const { data: res } = await pageButton(
        this.queryInfo.current,
        this.queryInfo.size
      );
      this.buttonList = res.data.records;
      this.total = res.data.total;
    },

    onSavaButton() {
      saveButton(this.addButtonForm)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.addButtonDialogVisible = false;
          this.getButtonList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //重置新增按钮填写的窗口
    addButtonDialogClosed() {
      this.$refs.addButtonRef.resetFields();
    },
    //跳出删除的弹错提醒
    removeButton(id) {
      getButtonById(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("无法删除");
          }

          this.buttonForm = res.data.data;
          this.removeButtonDialogVisible = true;
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //点击确认删除按钮
    onRemoveButton() {
      removeButtonById(this.buttonForm.id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("无法删除");
          }
          this.removeButtonDialogVisible = false;
          this.getButtonList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
  },
};
</script>

<style lang="less" scoped>
.breadcrumb {
  margin-bottom: 15px;
  font-size: 15px;
}
.box-card {
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15) !important;
}
.el-row-table {
  margin-bottom: 15px !important;
}
.el-pagination {
  margin-top: 15px;
}
</style>